package com.ldx.eduline.college.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldx.eduline.college.entity.EduTeacher;
import com.ldx.eduline.college.entity.param.EduTeacherParam;
import com.ldx.eduline.college.service.EduTeacherService;
import com.ldx.eduline.common.bean.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ldx
 * @since 2020-12-20
 */
@RestController
@Api("讲师管理")
@Slf4j
@RequestMapping("/college/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @PostMapping("{current}/{size}")
    @ApiOperation("讲师列表")
    public R list(@PathVariable("size") Long size, @PathVariable("current") Long current,
                  @RequestBody(required = false) EduTeacherParam eduTeacherParam) {
        Page<EduTeacher> page = new Page<>(current, size);
        IPage<EduTeacher> pageList = eduTeacherService.page
                (page, new LambdaQueryWrapper<EduTeacher>()
                        .like(StringUtils.isNotBlank(eduTeacherParam.getName()), EduTeacher::getName, eduTeacherParam.getName())
                        .orderByDesc(EduTeacher::getGmtCreate)
                );
        return R.success().addData("teacherList", pageList);
    }

    @GetMapping("{id}")
    public R getTeacherById(@PathVariable @ApiParam("id") String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.success().addData("teacher", eduTeacher);
    }

    @DeleteMapping("{id}")
    @ApiOperation("逻辑删除讲师")
    public R delTeacher(@PathVariable @ApiParam(name = "id", value = "讲师id", required = true) String id) {
        boolean b = eduTeacherService.removeById(id);
        return R.success();
    }

    @PostMapping
    @ApiOperation("添加讲师")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        eduTeacher.setGmtCreate(new Date());
        eduTeacher.setGmtModified(new Date());
        eduTeacherService.save(eduTeacher);
        return R.success();
    }

    @PostMapping("update")
    @ApiOperation("添加讲师")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        eduTeacherService.updateById(eduTeacher);
        return R.success();
    }
}

